package com.dove.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayUserDtbankcustChannelvoucherSendRequest;
import com.alipay.api.request.AlipayUserDtbankcustChannelvoucherconfigQueryRequest;
import com.alipay.api.response.AlipayUserDtbankcustChannelvoucherSendResponse;
import com.alipay.api.response.AlipayUserDtbankcustChannelvoucherconfigQueryResponse;
import com.dove.common.MyProperties;
import com.dove.utils.DemoUtil;

@RestController
@RequestMapping("/alipay")
public class AlipayController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private MyProperties myProperties;

	@GetMapping("/test")
	public String testget(String name) {
		return "hello" + name;
	}

	@PostMapping("/alipay.user.dtbankcust.channelvoucher.send")
	public String send(String user_id, String logon_id, String activity_id, String out_biz_no) {
		AlipayClient alipayClient = new DefaultAlipayClient(myProperties.getUrl(), myProperties.getApp_id(), myProperties.getApp_private_key(), "json", "UTF-8", myProperties.getAlipay_public_key(), "RSA2");
		AlipayUserDtbankcustChannelvoucherSendRequest request = new AlipayUserDtbankcustChannelvoucherSendRequest();

		request.setBizContent("{" + "\"user_id\":\"2088xxxxxxxxxxxx\"," + "\"logon_id\":\"131xxxxxxxx\"," + "\"activity_id\":\"ACT836CCV00020212\"," + "\"out_biz_no\":\"2020122814164328aadaee\"" + "  }");

		AlipayUserDtbankcustChannelvoucherSendResponse response = null;
		try {
			response = alipayClient.execute(request);
			DemoUtil.logs(logger, myProperties.getUrl(), JSON.toJSONString(request), JSON.toJSONString(response));
		} catch (AlipayApiException e) {
			DemoUtil.logs(logger, myProperties.getUrl(), JSON.toJSONString(request), JSON.toJSONString(response));
		}
		return JSON.toJSONString(response);
	}

	@PostMapping("/alipay.user.dtbankcust.channelvoucherconfig.query")
	public String query(String activity_id) {
		AlipayClient alipayClient = new DefaultAlipayClient(myProperties.getUrl(), myProperties.getApp_id(), myProperties.getApp_private_key(), "json", "UTF-8", myProperties.getAlipay_public_key(), "RSA2");
		AlipayUserDtbankcustChannelvoucherconfigQueryRequest request = new AlipayUserDtbankcustChannelvoucherconfigQueryRequest();

		request.setBizContent("{" + "\"activity_id\":\"ACT358CCV00013118\"" + "  }");

		AlipayUserDtbankcustChannelvoucherconfigQueryResponse response = null;
		try {
			response = alipayClient.execute(request);
			DemoUtil.logs(logger, myProperties.getUrl(), JSON.toJSONString(request), JSON.toJSONString(response));
		} catch (AlipayApiException e) {
			DemoUtil.logs(logger, myProperties.getUrl(), JSON.toJSONString(request), JSON.toJSONString(response));
		}
		return JSON.toJSONString(response);
	}

}
